//
// File  : Controller.cpp
// Team  : Dangling Pointers
// Class : Software Testing
//
// Controller class definitions
//

#include "Controller.h"
#include <iostream>
#include <sstream>
#include <cstdlib> // needed for atoi


int Controller::getUserInput(int max) 
{
	// get valid user input between one and max
	int numIn;

	while (true)
	{
		// read a string from standard input
		numIn = 0;
		std::cout << "Enter menu item between 1 and " << max << ": ";
		std::string input;
		getline(std::cin, input);

		// convert the string to a number
		std::istringstream ss(input);
		ss >> numIn;

		// if num is in the accepted range
		if (numIn >= 1 && numIn <= max) 
			break;

		// else
		std::cout << "Invalid entry! ";
	}

	return numIn;
}

//
// validateFlightNumber : bool
//
// method takes a string as a parameter and returns a boolean value. The method
// validates that the flight time is in the correct format (i.e. FXXX, where
// XXX is a numerical value)
//
bool Controller::validateFlightNumber(std::string flightNumber)
{	
	if(flightNumber.size() != 4)
		return false;
	if(flightNumber.substr(0,1) != "F")
		return false;

	//
	// make sure remaining three entries in the string are digits!
	//
	for(size_t i = 1; i < flightNumber.length(); ++i)
	{
		if( !isdigit(flightNumber[i]) )
			return false;
	}

	//
	// convert digit portion of flight number to integers before
	// comparison
	//
	int digit1 = atoi(flightNumber.substr(1,1).c_str());
	int digit2 = atoi(flightNumber.substr(2,1).c_str());
	int digit3 = atoi(flightNumber.substr(3,1).c_str());


	if( !(digit1 > -1 && digit1 < 10) )
		return false;
	if( !(digit2 > -1 && digit2 < 10) )
		return false;
	if( !(digit3 > -1 && digit3 < 10) )
		return false;

	return true;
}


void Controller::getFlightFromUser(std::string& flightNumber)
{
	std::cout << "Enter the flight number (in format FXXX): ";
	getline(std::cin, flightNumber);
}